package com.puzzlebazar.client.squaregrid.model;

import com.puzzlebazar.client.puzzle.model.Move;
import com.puzzlebazar.client.puzzle.model.MoveImpl;
import com.puzzlebazar.client.puzzle.model.MoveVisitor;
import com.puzzlebazar.client.puzzle.model.PuzzleModel;
import com.puzzlebazar.client.util.Vec2i;

public class MoveChangeState extends MoveImpl {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2698072687937342681L;
	
	
	private Vec2i loc;
	private int stateBefore;
	private int stateAfter;
	
	/**
	 * Only for serialization
	 */
	protected MoveChangeState() {
		super();
	}
	
	
	/**
	 * Create a move to represent a state change
	 * 
	 * @param loc  The location of the cell that changes
	 * @param stateBefore The state before the change
	 * @param stateAfter The state after the change
	 * @param time The time of the move
	 */
	public MoveChangeState( Vec2i loc, int stateBefore, int stateAfter, long time ) {
		super( time );
		this.loc = new Vec2i(loc);
		this.stateBefore = stateBefore;
		this.stateAfter = stateAfter;
	}
	
	@Override
	public void accept(MoveVisitor visitor) throws Exception {
		if( !(visitor instanceof StateMoveVisitor) ) 
			super.accept(visitor);
		else
			((StateMoveVisitor)visitor).visitChangeStateMove(this);		
	}

	@Override
	public Move apply(PuzzleModel model) throws Exception {
		assert( model instanceof StateGrid);
		return ((StateGrid)model).setState(loc, stateAfter, time);	
	}

	@Override
	public Move unapply(PuzzleModel model) throws Exception {
		assert( model instanceof StateGrid);
		return ((StateGrid)model).setState(loc, stateBefore, time);	
	}

	/**
	 * @return The state before this move is applied
	 */
	public int getStateBefore() {
		return stateBefore;
	}

	/**
	 * @return The state after this move is applied
	 */
	public int getStateAfter() {
		return stateAfter;
	}

	/**
	 * @return The location where the move takes place
	 */
	public Vec2i getLoc() {
		return new Vec2i(loc);
	}
}
